# encoding: utf-8
class ApplicationController < ActionController::Base
  protect_from_forgery

  def get_workpackage
    @workpackage = Workpackage.find(params[:workpackage_id])
  end

  def get_project
    @project = Project.find(params[:project_id])
  end

  def get_topic
    @topic = Topic.find(params[:topic_id])
  end

  def is_member_of_project?
    @id_to_compare = params[:project_id] ? params[:project_id] : params[:id]
    unless(!Member.where(:project_id => @id_to_compare, :user_id => current_user.id).empty?)
      flash[:error] = t :no_member, :scope => [:flash, :error]
      redirect_to root_path
    end
  end

  def is_project_manager?
		@id_to_compare = params[:project_id] ? params[:project_id] : params[:id]
    user_signed_in? and !Member.where( :project_id => @id_to_compare, :user_id => current_user.id, :role => "manager").empty?
  end

  def get_task
    @task = Task.find(params[:task_id])
  end

  def set_progress
    if !@workpackage.milestone && !@workpackage.tasks.empty?
      @tasks = @workpackage.tasks
      progw = 0
      planw = 0
      old_wp_status = @workpackage.status
      wp_status = 'finished'

      @tasks.each do |task|
        progw = progw + task.progress*task.planned
        planw = planw + task.planned

        if old_wp_status != @task.status
          if task.status == 'delayed'
            wp_status = 'delayed'
          end
        end
      end

      if old_wp_status != @task.status
        if wp_status == 'finished'
          @tasks.each do |task|
            if task.status == 'in_progress'
              wp_status = 'in_progress'
            end
          end
        end

        if wp_status == 'finished'
          @tasks.each do |task|
            if task.status == 'initial'
              wp_status = 'initial'
            end
          end
        end

        if wp_status == 'initial'
          @tasks.each do |task|
            if task.status == 'finished'
              wp_status = 'in_progress'
            end
          end
        end
      end

      progw = progw/planw

      @workpackage.update_attributes(:progress => progw.to_i)

      if old_wp_status != @task.status
        @workpackage.update_attributes(:status => wp_status)
      end
    end

    progp = 0
    @workpackages = @project.workpackages

    old_p_status = @project.status
    p_status = 'finished'

    @workpackages.each do |workpackage|
      progp = progp + workpackage.progress

      if old_p_status != wp_status
        if workpackage.status == 'delayed'
          p_status = 'delayed'
        end
      end
    end

    if old_p_status != wp_status
      if p_status == 'finished'
        @workpackages.each do |workpackage|
          if workpackage.status == 'in_progress'
            p_status = 'in_progress'
          end
        end
      end

      if p_status == 'finished'
        @workpackages.each do |workpackage|
          if workpackage.status == 'initial'
            p_status = 'initial'
          end
        end
      end

      if p_status == 'initial'
        @workpackages.each do |workpackage|
          if workpackage.status == 'finished'
            p_status = 'in_progress'
          end
        end
      end
    end

    progp = progp/@project.workpackages.size

    @project.update_attributes(:progress => progp.to_i)

    if old_p_status != wp_status
      @project.update_attributes(:status => p_status)
    end
  end

end
